package com.xiaoKe.zhonghzu.ui.fzxz;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.xiaoKe.zhonghzu.R;
import com.xiaoKe.zhonghzu.databinding.FragmentReproductiveDetailsBinding;
import com.xiaoKe.zhonghzu.global.base.BaseFragment;
import com.xiaoKe.zhonghzu.global.base.BaseResponseBean;
import com.xiaoKe.zhonghzu.global.base.VeinRouter;
import com.xiaoKe.zhonghzu.global.bean.ClFzData;
import com.xiaoKe.zhonghzu.global.bean.FzDataDetailsBean;
import com.xiaoKe.zhonghzu.global.okhttp.ApiCallback;
import com.xiaoKe.zhonghzu.global.okhttp.ApiCallbackOnResponse;
import com.xiaoKe.zhonghzu.global.okhttp.ApiService;
import com.xiaoKe.zhonghzu.global.okhttp.RetrofitClient;
import com.xiaoKe.zhonghzu.global.tool.DateUtils;
import com.xiaoKe.zhonghzu.global.tool.SPUtils;
import com.xiaoKe.zhonghzu.global.tool.ToastUtils;
import com.xiaoKe.zhonghzu.global.view.ViewTotHeader;
import com.xiaoKe.zhonghzu.ui.onClick.NoDoubleClickListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ProjectName : xiaoke-pad-zhongzhu
 * @Author : Shijingna
 * @Time : 2024/11/27 14:38
 * @Description : 繁殖性状
 */
public class DtjDetailsFragment extends BaseFragment<FragmentReproductiveDetailsBinding> {
    Long id;
    List<String> farrowingMethodList = new ArrayList<>();
    ClFzData clFzData;

    @Override
    protected void initView() {
        clFzData = (ClFzData) getArguments().getSerializable("data");
        if (null==clFzData){
            return;
        }
        farrowingMethodList.add("顺产");
        farrowingMethodList.add("早产");
        farrowingMethodList.add("难产");
        id = getArguments().getLong("id");
        ViewTotHeader viewTotHeader = getView().findViewById(R.id.viewTotHeader);
        viewTotHeader.into("数据信息", false);
    }

    @Override
    protected void initData() {
        NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View view) {
                switch (view.getId()) {
                    case R.id.delete:
                        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                        builder.setTitle("提示")
                                .setMessage("确定删除猪只" + clFzData.getPigCode() + "繁殖数据吗？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        delete();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 处理取消按钮的点击事件
                                    }
                                })
                                .setCancelable(false) // 设置对话框不可取消
                                .show(); // 显示对话框

                        break;
                    case R.id.upData:
                        Bundle bundle = new Bundle();
                        bundle.putString("titleName", VeinRouter.REPRODUCTIVEADDFRAGMENT.getTitle());
                        bundle.putSerializable("data", clFzData);
                        toFragmentPage(VeinRouter.REPRODUCTIVEADDFRAGMENT.setBundle(bundle));
                        getActivity().finish();
                        break;
                }
            }
        };
        getView().findViewById(R.id.upData).setOnClickListener(noDoubleClickListener);
        getView().findViewById(R.id.delete).setOnClickListener(noDoubleClickListener);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (null==clFzData){
            return;
        }
        getData();
    }

    public void getData() {
        // 设置数据到视图控件
        binding.farrowType.setText( farrowingMethodList.get(clFzData.getFarrowType()));
        binding.houseNo.setText(DateUtils.getContent(clFzData.getHouseNo()));
        binding.pigCode.setText(DateUtils.getContent(clFzData.getPigCode()));
        binding.pigBreed.setText(DateUtils.getContent(clFzData.getPigBreed()));
        binding.collector.setText(DateUtils.getContent(clFzData.getCollector()));
        binding.penNumber.setText(DateUtils.getContent(clFzData.getPenNo()));
        binding.farrowingDate.setText(DateUtils.getContent(clFzData.getFarrowDate()));
        binding.totalBorn.setText(DateUtils.getContent(clFzData.getTotalBorn()));
        binding.liveBorn.setText(DateUtils.getContent(clFzData.getLiveBorn()));
        binding.weakPiglet.setText(DateUtils.getContent(clFzData.getWeakPiglet()));
        binding.abnormal.setText(DateUtils.getContent(clFzData.getAbnormal()));
        binding.mummified.setText(DateUtils.getContent(clFzData.getMummified()));
        binding.deadPiglet.setText(DateUtils.getContent(clFzData.getDeadPiglet()));
        binding.live12h.setText(DateUtils.getContent(clFzData.getLive12h()));
        binding.live24h.setText(DateUtils.getContent(clFzData.getLive24h()));
        binding.live7d.setText(DateUtils.getContent(clFzData.getLive7d()));
        binding.weanWeight.setText(DateUtils.getContent(clFzData.getWeanWeight()));
        binding.weanPiglets.setText(DateUtils.getContent(clFzData.getWeanPiglets()));
        binding.rearedPiglets.setText(DateUtils.getContent(clFzData.getRearedPiglets()));
        binding.weight21d.setText(DateUtils.getContent(clFzData.getWeight21d()));
        binding.weight60d.setText(DateUtils.getContent(clFzData.getWeight60d()));
        binding.teatLeft.setText(DateUtils.getContent(clFzData.getTeatLeft()));
        binding.teatRight.setText(DateUtils.getContent(clFzData.getTeatRight()));
        binding.teatTotal.setText(DateUtils.getContent(clFzData.getTeatTotal()));
        binding.healthyPiglets.setText(DateUtils.getContent(clFzData.getHealthyPiglets()));
        binding.birthWeight.setText(DateUtils.getContent(clFzData.getBirthWeight()));
        binding.survivalRate.setText(DateUtils.getContent(clFzData.getSurvivalRate()));
        binding.firstFarrowAge.setText(DateUtils.getContent(clFzData.getFirstFarrowAge()));
        binding.birthWeanWeight.setText(DateUtils.getContent(clFzData.getBirthWeanWeight()));
        binding.parity.setText(DateUtils.getContent(clFzData.getParity()));
        binding.collectLocation.setText(DateUtils.getContent(clFzData.getCollectLocation()));
        binding.collectionDate.setText(DateUtils.getContent(clFzData.getCollectDate()));
        binding.houseNo.setText(DateUtils.getContent(clFzData.getHouseNo()));

    }

    public void delete() {
        List<ClFzData> dtjFragmentList = SPUtils.getDataList("DtjFragment", ClFzData.class);
        if (null!= dtjFragmentList) { //先删除重复的替换上最新数据
            dtjFragmentList.removeIf(fragment ->
                    Objects.equals(fragment.getPigCode(), clFzData.getPigCode()) &&
                            Objects.equals(fragment.getPigBreed(), clFzData.getPigBreed())
            );
        }
        SPUtils.putDataList("DtjFragment",dtjFragmentList );
        getActivity().finish();
    }
}
